<html>
<head><meta charset="utf-8"><title>NonNull and aliasing model · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model.html">NonNull and aliasing model</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="194542739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull%20and%20aliasing%20model/near/194542739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model.html#194542739">(Apr 18 2020 at 12:44)</a>:</h4>
<p>Hi,<br>
I'm reading <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/194" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/194">https://github.com/rust-lang/unsafe-code-guidelines/issues/194</a>, and I have a question, in that thread people keep talking about <code>NonNull</code> allowing aliasing.<br>
e.g:</p>
<blockquote>
<p>We replace the Vec with a deconstructed vector; it's equivalent but uses NonNull for its pointer, so it's an aliasable owner.</p>
</blockquote>
<p>and:</p>
<blockquote>
<p>the NonNull to opt out of uniqueness;</p>
</blockquote>
<p>Is there something special about NonNull that allows aliasing where "regular" raw pointers don't? or am I misunderstanding this?</p>
<p>Thanks!</p>



<a name="194547756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull%20and%20aliasing%20model/near/194547756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model.html#194547756">(Apr 18 2020 at 14:38)</a>:</h4>
<p>AFAIK <code>NonNull</code> has the same semantics as a raw pointer.</p>



<a name="194548013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull%20and%20aliasing%20model/near/194548013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model.html#194548013">(Apr 18 2020 at 14:44)</a>:</h4>
<p>The unstable <code>Unique&lt;T&gt;</code> pointer type may have additional aliasing constraints, but it's not very clear to me. And apparently <code>Box&lt;T&gt;</code> has the same restrictions since it contains a <code>Unique&lt;T&gt;</code>.</p>



<a name="194584468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull%20and%20aliasing%20model/near/194584468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model.html#194584468">(Apr 19 2020 at 07:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model/near/194547756" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model/near/194547756">said</a>:</p>
<blockquote>
<p>AFAIK <code>NonNull</code> has the same semantics as a raw pointer.</p>
</blockquote>
<p>agreed</p>



<a name="194584514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull%20and%20aliasing%20model/near/194584514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model.html#194584514">(Apr 19 2020 at 07:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model/near/194548013" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/NonNull.20and.20aliasing.20model/near/194548013">said</a>:</p>
<blockquote>
<p>The unstable <code>Unique&lt;T&gt;</code> pointer type may have additional aliasing constraints, but it's not very clear to me. And apparently <code>Box&lt;T&gt;</code> has the same restrictions since it contains a <code>Unique&lt;T&gt;</code>.</p>
</blockquote>
<p>Technically right now as implemented in Miri, <code>Box</code> has special constraints, but <code>Unique</code> does not. However this is something we certainly want to explore in the future. I'd very much like to find a way to make <code>Unique</code> special, and get corresponding optimizations for <code>Vec</code>.<br>
Conceptually, indeed <code>Unique</code> is special and hence deconstructing a <code>Vec</code> into a <code>NonNull</code> <em>does</em> make a difference.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>